# coding:utf-8
import json
import time
from .. import models


class QueryData:

    def __init__(self):
        self.tokens = lambda token: True if int(token) + 86400 > int(time.time()) else False
        self.start_date = '0000-00-00 00:00:00'
        self.end_date = '9999-00-00 00:00:00'

    def user_login(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    userinfo = models.automation_user.objects.get(username=request.POST['username'],
                                                                  password=request.POST['password'])
                    if userinfo.userstatus == "1":
                        usermessage = {
                            "userid": userinfo.id,
                            "username": userinfo.username,
                            "nickname": userinfo.nickname,
                            "userstatus": userinfo.userstatus,
                            "createdtime": userinfo.createdtime,
                            "updatetime": userinfo.updatetime
                        }
                        login_return_data = {"code": "200", "msg": "success", "data": usermessage}
                    elif userinfo.userstatus == "0":
                        login_return_data = {"code": "100", "msg": "该用户账号已经被禁用，请联系管理员。", "data": {}}
                    else:
                        login_return_data = {"code": "101", "msg": "该用户账号异常，请联系管理员。", "data": {}}
                except:
                    login_return_data = {"code": "102", "msg": "账号或密码错误", "data": {}}
            else:
                login_return_data = {"code": "-11", "msg": "token过期", "data": {}}
        else:
            login_return_data = {"code": "-12", "msg": "请求方式错误", "data": {}}
        return json.dumps(login_return_data)

    def cpu_rate(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    querydatas = models.automation_cpu_app.objects.all()
                    querydatalist = []
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    if request.POST['eventid'] == '':
                        # 判断eventid 是否为空
                        if request.POST['caseid'] == '':
                            for querydata in range(0, len(querydatas)):
                                if querydatas[querydata].createdtime <= self.end_date and querydatas[querydata].createdtime >= self.start_date:
                                    cpu_querydata = {
                                        "id": querydatas[querydata].id,
                                        "caseid": querydatas[querydata].caseid,
                                        "cpuproportion": querydatas[querydata].cpuproportion,
                                        "starttime": querydatas[querydata].starttime,
                                        "endtime": querydatas[querydata].endtime,
                                        "monkeyscript": querydatas[querydata].monkeyscript,
                                        "functionscript": querydatas[querydata].functionscript,
                                        "runtime": querydatas[querydata].runtime,
                                        "eventid": querydatas[querydata].eventid,
                                        "createdtime": querydatas[querydata].createdtime
                                    }
                                    querydatalist.append(cpu_querydata)
                        else:
                            for querydata in range(0, len(querydatas)):
                                if querydatas[querydata].createdtime <= self.end_date and querydatas[querydata].createdtime >= self.start_date:
                                    try:
                                        if request.POST['caseid'] == str(querydatas[querydata].caseid) or request.POST['caseid'] == int(querydatas[querydata].caseid):
                                            cpu_querydata = {
                                                "id": querydatas[querydata].id,
                                                "caseid": querydatas[querydata].caseid,
                                                "cpuproportion": querydatas[querydata].cpuproportion,
                                                "starttime": querydatas[querydata].starttime,
                                                "endtime": querydatas[querydata].endtime,
                                                "monkeyscript": querydatas[querydata].monkeyscript,
                                                "functionscript": querydatas[querydata].functionscript,
                                                "runtime": querydatas[querydata].runtime,
                                                "eventid": querydatas[querydata].eventid,
                                                "createdtime": querydatas[querydata].createdtime
                                            }
                                            querydatalist.append(cpu_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for querydata in range(0, len(querydatas)):
                                if querydatas[querydata].createdtime <= self.end_date and querydatas[
                                    querydata].createdtime >= self.start_date and request.POST['eventid'] in querydatas[
                                    querydata].eventid:
                                    cpu_querydata = {
                                        "id": querydatas[querydata].id,
                                        "caseid": querydatas[querydata].caseid,
                                        "cpuproportion": querydatas[querydata].cpuproportion,
                                        "starttime": querydatas[querydata].starttime,
                                        "endtime": querydatas[querydata].endtime,
                                        "monkeyscript": querydatas[querydata].monkeyscript,
                                        "functionscript": querydatas[querydata].functionscript,
                                        "runtime": querydatas[querydata].runtime,
                                        "eventid": querydatas[querydata].eventid,
                                        "createdtime": querydatas[querydata].createdtime
                                    }
                                    querydatalist.append(cpu_querydata)
                        else:
                            for querydata in range(0, len(querydatas)):
                                if querydatas[querydata].createdtime <= self.end_date and querydatas[
                                    querydata].createdtime >= self.start_date and request.POST['eventid'] in querydatas[
                                    querydata].eventid:
                                    try:
                                        if request.POST['caseid'] == str(querydatas[querydata].caseid) or request.POST['caseid'] == int(querydatas[querydata].caseid):
                                            cpu_querydata = {
                                                "id": querydatas[querydata].id,
                                                "caseid": querydatas[querydata].caseid,
                                                "cpuproportion": querydatas[querydata].cpuproportion,
                                                "starttime": querydatas[querydata].starttime,
                                                "endtime": querydatas[querydata].endtime,
                                                "monkeyscript": querydatas[querydata].monkeyscript,
                                                "functionscript": querydatas[querydata].functionscript,
                                                "runtime": querydatas[querydata].runtime,
                                                "eventid": querydatas[querydata].eventid,
                                                "createdtime": querydatas[querydata].createdtime
                                            }
                                            querydatalist.append(cpu_querydata)
                                    except:
                                        pass
                    cpu_rate_data = json.dumps({"code": "200", "msg": "succes", "data": querydatalist})
                except:
                    cpu_rate_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                cpu_rate_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
        else:
            cpu_rate_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return cpu_rate_data

    def launch_app(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    launchappdatas = models.automation_launch_app.objects.all()
                    launchappdatalist = []
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    if request.POST['eventid'] == '':
                        if request.POST['caseid'] == '':
                            for launchappdata in range(0, len(launchappdatas)):
                                if launchappdatas[launchappdata].createdtime <= self.end_date and launchappdatas[launchappdata].createdtime >= self.start_date:
                                    cpu_querydata = {
                                        "id": launchappdatas[launchappdata].id,
                                        "starttime": launchappdatas[launchappdata].starttime,
                                        "launchtime": launchappdatas[launchappdata].launchtime,
                                        "endtime": launchappdatas[launchappdata].endtime,
                                        "launchtype": launchappdatas[launchappdata].launchtype,
                                        "eventid": launchappdatas[launchappdata].eventid,
                                        "caseid": launchappdatas[launchappdata].caseid,
                                        "createdtime": launchappdatas[launchappdata].createdtime
                                    }
                                    launchappdatalist.append(cpu_querydata)
                        else:
                            for launchappdata in range(0, len(launchappdatas)):
                                if launchappdatas[launchappdata].createdtime <= self.end_date and launchappdatas[launchappdata].createdtime >= self.start_date:
                                    try:
                                        if request.POST['caseid'] == int(launchappdatas[launchappdata].id) or request.POST['caseid'] == str(launchappdatas[launchappdata].caseid):
                                            cpu_querydata = {
                                                "id": launchappdatas[launchappdata].id,
                                                "starttime": launchappdatas[launchappdata].starttime,
                                                "launchtime": launchappdatas[launchappdata].launchtime,
                                                "endtime": launchappdatas[launchappdata].endtime,
                                                "launchtype": launchappdatas[launchappdata].launchtype,
                                                "eventid": launchappdatas[launchappdata].eventid,
                                                "caseid": launchappdatas[launchappdata].caseid,
                                                "createdtime": launchappdatas[launchappdata].createdtime
                                            }
                                            launchappdatalist.append(cpu_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for launchappdata in range(0, len(launchappdatas)):
                                if launchappdatas[launchappdata].createdtime <= self.end_date and launchappdatas[launchappdata].createdtime >= self.start_date and request.POST[
                                    'eventid'] in \
                                        launchappdatas[launchappdata].eventid:
                                    cpu_querydata = {
                                        "id": launchappdatas[launchappdata].id,
                                        "starttime": launchappdatas[launchappdata].starttime,
                                        "launchtime": launchappdatas[launchappdata].launchtime,
                                        "endtime": launchappdatas[launchappdata].endtime,
                                        "launchtype": launchappdatas[launchappdata].launchtype,
                                        "eventid": launchappdatas[launchappdata].eventid,
                                        "caseid": launchappdatas[launchappdata].caseid,
                                        "createdtime": launchappdatas[launchappdata].createdtime
                                    }
                                    launchappdatalist.append(cpu_querydata)
                        else:
                            for launchappdata in range(0, len(launchappdatas)):
                                if launchappdatas[launchappdata].createdtime <= self.end_date and launchappdatas[launchappdata].createdtime >= self.start_date and \
                                        request.POST['eventid'] in launchappdatas[launchappdata].eventid:
                                    try:
                                        if request.POST['caseid'] == int(launchappdatas[launchappdata].id) or request.POST['caseid'] == str(launchappdatas[launchappdata].caseid):
                                            cpu_querydata = {
                                                "id": launchappdatas[launchappdata].id,
                                                "starttime": launchappdatas[launchappdata].starttime,
                                                "launchtime": launchappdatas[launchappdata].launchtime,
                                                "endtime": launchappdatas[launchappdata].endtime,
                                                "launchtype": launchappdatas[launchappdata].launchtype,
                                                "eventid": launchappdatas[launchappdata].eventid,
                                                "caseid": launchappdatas[launchappdata].caseid,
                                                "createdtime": launchappdatas[launchappdata].createdtime
                                            }
                                            launchappdatalist.append(cpu_querydata)
                                    except:
                                        pass
                    launch_app_data = json.dumps({"code": "200", "msg": "succes", "data": launchappdatalist})
                except:
                    launch_app_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                launch_app_data = {"code": "-11", "msg": "token过期", "data": {}}
        else:
            launch_app_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})

        return launch_app_data

    def memory_app(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    menappdatas = models.automation_mem_app.objects.all()
                    menappdataslist = []
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    if request.POST['eventid'] == '':
                        if request.POST['caseid'] == '':
                            for menappdata in range(0, len(menappdatas)):
                                if menappdatas[menappdata].createdtime >= self.start_date and menappdatas[
                                    menappdata].createdtime <= self.end_date:
                                    menapp_querydata = {
                                        "id": menappdatas[menappdata].id,
                                        "caseid": menappdatas[menappdata].caseid,
                                        "starttime": menappdatas[menappdata].starttime,
                                        "endtime": menappdatas[menappdata].endtime,
                                        "memorysize": menappdatas[menappdata].memorysize,
                                        "monkeyscript": menappdatas[menappdata].monkeyscript,
                                        "functionscript": menappdatas[menappdata].functionscript,
                                        "runtime": menappdatas[menappdata].runtime,
                                        "eventid": menappdatas[menappdata].eventid,
                                        "createdtime": menappdatas[menappdata].createdtime,
                                    }
                                    menappdataslist.append(menapp_querydata)
                        else:
                            for menappdata in range(0, len(menappdatas)):
                                if menappdatas[menappdata].createdtime >= self.start_date and menappdatas[
                                    menappdata].createdtime <= self.end_date:
                                    try:
                                        if request.POST['caseid'] == int(menappdatas[menappdata].caseid) or request.POST['caseid'] == str(menappdatas[menappdata].caseid):
                                            menapp_querydata = {
                                                "id": menappdatas[menappdata].id,
                                                "caseid": menappdatas[menappdata].caseid,
                                                "starttime": menappdatas[menappdata].starttime,
                                                "endtime": menappdatas[menappdata].endtime,
                                                "memorysize": menappdatas[menappdata].memorysize,
                                                "monkeyscript": menappdatas[menappdata].monkeyscript,
                                                "functionscript": menappdatas[menappdata].functionscript,
                                                "runtime": menappdatas[menappdata].runtime,
                                                "eventid": menappdatas[menappdata].eventid,
                                                "createdtime": menappdatas[menappdata].createdtime,
                                            }
                                            menappdataslist.append(menapp_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for menappdata in range(0, len(menappdatas)):
                                if menappdatas[menappdata].createdtime >= self.start_date and menappdatas[menappdata].createdtime <= self.end_date and request.POST['eventid'] in \
                                        menappdatas[
                                            menappdata].eventid:
                                    menapp_querydata = {
                                        "id": menappdatas[menappdata].id,
                                        "caseid": menappdatas[menappdata].caseid,
                                        "starttime": menappdatas[menappdata].starttime,
                                        "endtime": menappdatas[menappdata].endtime,
                                        "memorysize": menappdatas[menappdata].memorysize,
                                        "monkeyscript": menappdatas[menappdata].monkeyscript,
                                        "functionscript": menappdatas[menappdata].functionscript,
                                        "runtime": menappdatas[menappdata].runtime,
                                        "eventid": menappdatas[menappdata].eventid,
                                        "createdtime": menappdatas[menappdata].createdtime,
                                    }
                                    menappdataslist.append(menapp_querydata)
                        else:
                            for menappdata in range(0, len(menappdatas)):
                                if menappdatas[menappdata].createdtime >= self.start_date and menappdatas[menappdata].createdtime <= self.end_date and request.POST['eventid'] in \
                                        menappdatas[menappdata].eventid:
                                    try:
                                        if request.POST['caseid'] == int(menappdatas[menappdata].caseid) or request.POST['caseid'] == str(menappdatas[menappdata].caseid):
                                            menapp_querydata = {
                                                "id": menappdatas[menappdata].id,
                                                "caseid": menappdatas[menappdata].caseid,
                                                "starttime": menappdatas[menappdata].starttime,
                                                "endtime": menappdatas[menappdata].endtime,
                                                "memorysize": menappdatas[menappdata].memorysize,
                                                "monkeyscript": menappdatas[menappdata].monkeyscript,
                                                "functionscript": menappdatas[menappdata].functionscript,
                                                "runtime": menappdatas[menappdata].runtime,
                                                "eventid": menappdatas[menappdata].eventid,
                                                "createdtime": menappdatas[menappdata].createdtime,
                                            }
                                            menappdataslist.append(menapp_querydata)
                                    except:
                                        pass
                    memory_app_data = json.dumps({"code": "200", "msg": "succes", "data": menappdataslist})
                except:
                    memory_app_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                memory_app_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
        else:
            memory_app_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return memory_app_data

    def interface_query(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    interfacedatas = models.automation_interface.objects.all()
                    interfacedatalist = []
                    if request.POST['eventid'] == '':
                        if request.POST['caseid'] == '':
                            for interfacedata in range(0, len(interfacedatas)):
                                if interfacedatas[interfacedata].createdtime >= self.start_date and interfacedatas[interfacedata].createdtime <= self.end_date:
                                    interface_querydata = {
                                        "id": interfacedatas[interfacedata].id,
                                        "caseid": interfacedatas[interfacedata].caseid,
                                        "interfaceurl": interfacedatas[interfacedata].interfaceurl,
                                        "requestparameter": interfacedatas[interfacedata].id,
                                        "returnparameter": interfacedatas[interfacedata].requestparameter,
                                        "requesttype": interfacedatas[interfacedata].requesttype,
                                        "casestatus": interfacedatas[interfacedata].casestatus,
                                        "remark": interfacedatas[interfacedata].remark,
                                        "eventid": interfacedatas[interfacedata].eventid,
                                        "createdtime": interfacedatas[interfacedata].createdtime,
                                    }
                                    interfacedatalist.append(interface_querydata)
                        else:
                            for interfacedata in range(0, len(interfacedatas)):
                                if interfacedatas[interfacedata].createdtime >= self.start_date and interfacedatas[interfacedata].createdtime <= self.end_date:
                                    try:
                                        if request.POST['caseid'] == int(interfacedatas[interfacedata].caseid) or request.POST['caseid'] == str(
                                                interfacedatas[interfacedata].caseid):
                                            interface_querydata = {
                                                "id": interfacedatas[interfacedata].id,
                                                "caseid": interfacedatas[interfacedata].caseid,
                                                "interfaceurl": interfacedatas[interfacedata].interfaceurl,
                                                "requestparameter": interfacedatas[interfacedata].id,
                                                "returnparameter": interfacedatas[interfacedata].requestparameter,
                                                "requesttype": interfacedatas[interfacedata].requesttype,
                                                "casestatus": interfacedatas[interfacedata].casestatus,
                                                "remark": interfacedatas[interfacedata].remark,
                                                "eventid": interfacedatas[interfacedata].eventid,
                                                "createdtime": interfacedatas[interfacedata].createdtime,
                                            }
                                            interfacedatalist.append(interface_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for interfacedata in range(0, len(interfacedatas)):
                                if interfacedatas[interfacedata].createdtime >= self.start_date and interfacedatas[interfacedata].createdtime <= self.end_date and request.POST[
                                    'eventid'] in \
                                        interfacedatas[interfacedata].eventid:
                                    interface_querydata = {
                                        "id": interfacedatas[interfacedata].id,
                                        "caseid": interfacedatas[interfacedata].caseid,
                                        "interfaceurl": interfacedatas[interfacedata].interfaceurl,
                                        "requestparameter": interfacedatas[interfacedata].id,
                                        "returnparameter": interfacedatas[interfacedata].requestparameter,
                                        "requesttype": interfacedatas[interfacedata].requesttype,
                                        "casestatus": interfacedatas[interfacedata].casestatus,
                                        "remark": interfacedatas[interfacedata].remark,
                                        "eventid": interfacedatas[interfacedata].eventid,
                                        "createdtime": interfacedatas[interfacedata].createdtime,
                                    }
                                    interfacedatalist.append(interface_querydata)
                        else:
                            for interfacedata in range(0, len(interfacedatas)):
                                if interfacedatas[interfacedata].createdtime >= self.start_date and interfacedatas[interfacedata].createdtime <= self.end_date and request.POST[
                                    'eventid'] in \
                                        interfacedatas[interfacedata].eventid:
                                    try:
                                        if request.POST['caseid'] == int(interfacedatas[interfacedata].caseid) or request.POST['caseid'] == str(
                                                interfacedatas[interfacedata].caseid):
                                            interface_querydata = {
                                                "id": interfacedatas[interfacedata].id,
                                                "caseid": interfacedatas[interfacedata].caseid,
                                                "interfaceurl": interfacedatas[interfacedata].interfaceurl,
                                                "requestparameter": interfacedatas[interfacedata].id,
                                                "returnparameter": interfacedatas[interfacedata].requestparameter,
                                                "requesttype": interfacedatas[interfacedata].requesttype,
                                                "casestatus": interfacedatas[interfacedata].casestatus,
                                                "remark": interfacedatas[interfacedata].remark,
                                                "eventid": interfacedatas[interfacedata].eventid,
                                                "createdtime": interfacedatas[interfacedata].createdtime,
                                            }
                                            interfacedatalist.append(interface_querydata)
                                    except:
                                        pass
                    interface_query_data = json.dumps({"code": "200", "msg": "succes", "data": interfacedatalist})
                except:
                    interface_query_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                interface_query_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
        else:
            interface_query_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return interface_query_data

    def function_app_query(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    functionappdatas = models.automation_function_app.objects.all()
                    functionappdatalist = []
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    if request.POST['eventid'] == '':
                        if request.POST['caseid'] == '':
                            for functionappdata in range(0, len(functionappdatas)):
                                if functionappdatas[functionappdata].createdtime >= self.start_date and functionappdatas[
                                    functionappdata].createdtime <= self.end_date:
                                    functionapp_querydata = {
                                        "id": functionappdatas[functionappdata].id,
                                        "caseid": functionappdatas[functionappdata].caseid,
                                        "devicesinfos": functionappdatas[functionappdata].devicesinfos,
                                        "appiumport": functionappdatas[functionappdata].appiumport,
                                        "devicesexecute": functionappdatas[functionappdata].devicesexecute,
                                        "operatetype": functionappdatas[functionappdata].operatetype,
                                        "parameter": functionappdatas[functionappdata].parameter,
                                        "waittime": functionappdatas[functionappdata].waittime,
                                        "element": functionappdatas[functionappdata].element,
                                        "rundescribe": functionappdatas[functionappdata].rundescribe,
                                        "casereport": functionappdatas[functionappdata].casereport,
                                        "caseexecute": functionappdatas[functionappdata].caseexecute,
                                        "runcasetime": functionappdatas[functionappdata].runcasetime,
                                        "eventid": functionappdatas[functionappdata].eventid,
                                        "createdtime": functionappdatas[functionappdata].createdtime,
                                    }
                                    functionappdatalist.append(functionapp_querydata)
                        else:
                            for functionappdata in range(0, len(functionappdatas)):
                                if functionappdatas[functionappdata].createdtime >= self.start_date and functionappdatas[
                                    functionappdata].createdtime <= self.end_date:
                                    try:
                                        if str(functionappdatas[functionappdata].caseid) == request.POST['caseid'] or int(functionappdatas[functionappdata].caseid) == request.POST[
                                            'caseid']:
                                            functionapp_querydata = {
                                                "id": functionappdatas[functionappdata].id,
                                                "caseid": functionappdatas[functionappdata].caseid,
                                                "devicesinfos": functionappdatas[functionappdata].devicesinfos,
                                                "appiumport": functionappdatas[functionappdata].appiumport,
                                                "devicesexecute": functionappdatas[functionappdata].devicesexecute,
                                                "operatetype": functionappdatas[functionappdata].operatetype,
                                                "parameter": functionappdatas[functionappdata].parameter,
                                                "waittime": functionappdatas[functionappdata].waittime,
                                                "element": functionappdatas[functionappdata].element,
                                                "rundescribe": functionappdatas[functionappdata].rundescribe,
                                                "casereport": functionappdatas[functionappdata].casereport,
                                                "caseexecute": functionappdatas[functionappdata].caseexecute,
                                                "runcasetime": functionappdatas[functionappdata].runcasetime,
                                                "eventid": functionappdatas[functionappdata].eventid,
                                                "createdtime": functionappdatas[functionappdata].createdtime,
                                            }
                                            functionappdatalist.append(functionapp_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for functionappdata in range(0, len(functionappdatas)):
                                if functionappdatas[functionappdata].createdtime >= self.start_date and functionappdatas[
                                    functionappdata].createdtime <= self.end_date and request.POST['eventid'] in functionappdatas[
                                    functionappdata].eventid:
                                    functionapp_querydata = {
                                        "id": functionappdatas[functionappdata].id,
                                        "caseid": functionappdatas[functionappdata].caseid,
                                        "devicesinfos": functionappdatas[functionappdata].devicesinfos,
                                        "appiumport": functionappdatas[functionappdata].appiumport,
                                        "devicesexecute": functionappdatas[functionappdata].devicesexecute,
                                        "operatetype": functionappdatas[functionappdata].operatetype,
                                        "parameter": functionappdatas[functionappdata].parameter,
                                        "waittime": functionappdatas[functionappdata].waittime,
                                        "element": functionappdatas[functionappdata].element,
                                        "rundescribe": functionappdatas[functionappdata].rundescribe,
                                        "casereport": functionappdatas[functionappdata].casereport,
                                        "caseexecute": functionappdatas[functionappdata].caseexecute,
                                        "runcasetime": functionappdatas[functionappdata].runcasetime,
                                        "eventid": functionappdatas[functionappdata].eventid,
                                        "createdtime": functionappdatas[functionappdata].createdtime,
                                    }
                                    functionappdatalist.append(functionapp_querydata)
                        else:
                            for functionappdata in range(0, len(functionappdatas)):
                                if functionappdatas[functionappdata].createdtime >= self.start_date and functionappdatas[
                                    functionappdata].createdtime <= self.end_date and request.POST['eventid'] in functionappdatas[
                                    functionappdata].eventid:
                                    try:
                                        if str(functionappdatas[functionappdata].caseid) == request.POST['caseid'] or int(functionappdatas[functionappdata].caseid) == request.POST[
                                            'caseid']:
                                            functionapp_querydata = {
                                                "id": functionappdatas[functionappdata].id,
                                                "caseid": functionappdatas[functionappdata].caseid,
                                                "devicesinfos": functionappdatas[functionappdata].devicesinfos,
                                                "appiumport": functionappdatas[functionappdata].appiumport,
                                                "devicesexecute": functionappdatas[functionappdata].devicesexecute,
                                                "operatetype": functionappdatas[functionappdata].operatetype,
                                                "parameter": functionappdatas[functionappdata].parameter,
                                                "waittime": functionappdatas[functionappdata].waittime,
                                                "element": functionappdatas[functionappdata].element,
                                                "rundescribe": functionappdatas[functionappdata].rundescribe,
                                                "casereport": functionappdatas[functionappdata].casereport,
                                                "caseexecute": functionappdatas[functionappdata].caseexecute,
                                                "runcasetime": functionappdatas[functionappdata].runcasetime,
                                                "eventid": functionappdatas[functionappdata].eventid,
                                                "createdtime": functionappdatas[functionappdata].createdtime,
                                            }
                                            functionappdatalist.append(functionapp_querydata)
                                    except:
                                        pass
                    function_app_data = json.dumps({"code": "200", "msg": "succes", "data": functionappdatalist})
                except:
                    function_app_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                function_app_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
        else:
            function_app_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return function_app_data

    def function_web_query(self, request):
        if request.POST:
            if self.tokens(request.POST['token']):
                try:
                    functionwebdatas = models.automation_function_web.objects.all()
                    functionwebdatalist = []
                    if request.POST['startdate'] != '' and request.POST['enddate'] != '':
                        startdate = request.POST['startdate']
                        enddate = request.POST['enddate']
                    if request.POST['eventid'] == '':
                        if request.POST['caseid'] == '':
                            for functionwebdata in range(0, len(functionwebdatas)):
                                if functionwebdatas[functionwebdata].createdtime >= self.start_date and functionwebdatas[
                                    functionwebdata].createdtime <= self.end_date:
                                    functionweb_querydata = {
                                        "id": functionwebdatas[functionwebdata].id,
                                        "caseid": functionwebdatas[functionwebdata].caseid,
                                        "browsername": functionwebdatas[functionwebdata].browsername,
                                        "browserconfigure": functionwebdatas[functionwebdata].browserconfigure,
                                        "browserstatus": functionwebdatas[functionwebdata].browserstatus,
                                        "operatetype": functionwebdatas[functionwebdata].operatetype,
                                        "element": functionwebdatas[functionwebdata].element,
                                        "parameter": functionwebdatas[functionwebdata].parameter,
                                        "waittime": functionwebdatas[functionwebdata].waittime,
                                        "rundescribe": functionwebdatas[functionwebdata].rundescribe,
                                        "casereport": functionwebdatas[functionwebdata].casereport,
                                        "caseexecute": functionwebdatas[functionwebdata].caseexecute,
                                        "runcasetime": functionwebdatas[functionwebdata].runcasetime,
                                        "eventid": functionwebdatas[functionwebdata].eventid,
                                        "createdtime": functionwebdatas[functionwebdata].createdtime,
                                    }
                                    functionwebdatalist.append(functionweb_querydata)
                        else:
                            for functionwebdata in range(0, len(functionwebdatas)):
                                if functionwebdatas[functionwebdata].createdtime >= self.start_date and functionwebdatas[
                                    functionwebdata].createdtime <= self.end_date:
                                    try:
                                        if request.POST['caseid'] == str(functionwebdatas[functionwebdata].caseid) or request.POST['caseid'] == int(
                                                functionwebdatas[functionwebdata].caseid):
                                            functionweb_querydata = {
                                                "id": functionwebdatas[functionwebdata].id,
                                                "caseid": functionwebdatas[functionwebdata].caseid,
                                                "browsername": functionwebdatas[functionwebdata].browsername,
                                                "browserconfigure": functionwebdatas[functionwebdata].browserconfigure,
                                                "browserstatus": functionwebdatas[functionwebdata].browserstatus,
                                                "operatetype": functionwebdatas[functionwebdata].operatetype,
                                                "element": functionwebdatas[functionwebdata].element,
                                                "parameter": functionwebdatas[functionwebdata].parameter,
                                                "waittime": functionwebdatas[functionwebdata].waittime,
                                                "rundescribe": functionwebdatas[functionwebdata].rundescribe,
                                                "casereport": functionwebdatas[functionwebdata].casereport,
                                                "caseexecute": functionwebdatas[functionwebdata].caseexecute,
                                                "runcasetime": functionwebdatas[functionwebdata].runcasetime,
                                                "eventid": functionwebdatas[functionwebdata].eventid,
                                                "createdtime": functionwebdatas[functionwebdata].createdtime,
                                            }
                                            functionwebdatalist.append(functionweb_querydata)
                                    except:
                                        pass
                    else:
                        if request.POST['caseid'] == '':
                            for functionwebdata in range(0, len(functionwebdatas)):
                                if functionwebdatas[functionwebdata].createdtime >= self.start_date and functionwebdatas[
                                    functionwebdata].createdtime <= self.end_date and request.POST['eventid'] in functionwebdatas[functionwebdata].eventid:
                                    functionweb_querydata = {
                                        "id": functionwebdatas[functionwebdata].id,
                                        "caseid": functionwebdatas[functionwebdata].caseid,
                                        "browsername": functionwebdatas[functionwebdata].browsername,
                                        "browserconfigure": functionwebdatas[functionwebdata].browserconfigure,
                                        "browserstatus": functionwebdatas[functionwebdata].browserstatus,
                                        "operatetype": functionwebdatas[functionwebdata].operatetype,
                                        "element": functionwebdatas[functionwebdata].element,
                                        "parameter": functionwebdatas[functionwebdata].parameter,
                                        "waittime": functionwebdatas[functionwebdata].waittime,
                                        "rundescribe": functionwebdatas[functionwebdata].rundescribe,
                                        "casereport": functionwebdatas[functionwebdata].casereport,
                                        "caseexecute": functionwebdatas[functionwebdata].caseexecute,
                                        "runcasetime": functionwebdatas[functionwebdata].runcasetime,
                                        "eventid": functionwebdatas[functionwebdata].eventid,
                                        "createdtime": functionwebdatas[functionwebdata].createdtime,
                                    }
                                    functionwebdatalist.append(functionweb_querydata)
                        else:
                            for functionwebdata in range(0, len(functionwebdatas)):
                                if functionwebdatas[functionwebdata].createdtime >= self.start_date and functionwebdatas[
                                    functionwebdata].createdtime <= self.end_date and request.POST['eventid'] in functionwebdatas[functionwebdata].eventid:
                                    try:
                                        if request.POST['caseid'] == str(functionwebdatas[functionwebdata].caseid) or request.POST['caseid'] == int(
                                                functionwebdatas[functionwebdata].caseid):
                                            functionweb_querydata = {
                                                "id": functionwebdatas[functionwebdata].id,
                                                "caseid": functionwebdatas[functionwebdata].caseid,
                                                "browsername": functionwebdatas[functionwebdata].browsername,
                                                "browserconfigure": functionwebdatas[functionwebdata].browserconfigure,
                                                "browserstatus": functionwebdatas[functionwebdata].browserstatus,
                                                "operatetype": functionwebdatas[functionwebdata].operatetype,
                                                "element": functionwebdatas[functionwebdata].element,
                                                "parameter": functionwebdatas[functionwebdata].parameter,
                                                "waittime": functionwebdatas[functionwebdata].waittime,
                                                "rundescribe": functionwebdatas[functionwebdata].rundescribe,
                                                "casereport": functionwebdatas[functionwebdata].casereport,
                                                "caseexecute": functionwebdatas[functionwebdata].caseexecute,
                                                "runcasetime": functionwebdatas[functionwebdata].runcasetime,
                                                "eventid": functionwebdatas[functionwebdata].eventid,
                                                "createdtime": functionwebdatas[functionwebdata].createdtime,
                                            }
                                            functionwebdatalist.append(functionweb_querydata)
                                    except:
                                        pass
                    web_query_data = json.dumps({"code": "200", "msg": "succes", "data": functionwebdatalist})
                except:
                    web_query_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
            else:
                web_query_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
        else:
            web_query_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return web_query_data

    # chrome driver版本增加和获取
    def chrome_driver_info(self, request):
        if request.GET:
            device_info = models.automation_chrome_driver.objects.filter(deviceid=request.GET.get('deviceid')).order_by('-id')
            if device_info:
                chrome_driver_result = {
                    'deviceid': device_info[0].deviceid,
                    'drivername': device_info[0].drivername
                }
                chrome_driver_info_data = json.dumps({"code": "200", "msg": "success", "data": chrome_driver_result})
            else:
                return json.dumps({"code": "200", "msg": "success", "data": {}})
        elif request.POST:
            if request.POST['deviceid'] and request.POST['deviceversion'] and request.POST['drivername'] and request.POST['devicemodel']:
                crteatedtime = time.strftime('%Y-%m-%d %H:%M:%S')
                models.automation_chrome_driver(deviceid=request.POST['deviceid'], deviceversion=request.POST['deviceversion'], drivername=request.POST['drivername'],
                                                devicemodel=request.POST['devicemodel'], macaddress=request.POST['macaddress'], createdtime=crteatedtime).save()
                return json.dumps({"code": "200", "msg": "success", "data": {}})
        else:
            chrome_driver_info_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return chrome_driver_info_data
